#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Node {
    char ch;
    double freq;
    struct Node *lchild, *rchild;
} Node;

typedef struct Heap {
    Node **data;
    int n, cnt;
} heap;

Node *getNewNode(char ch, double freq, Node *lchild, Node *rchild) {
    Node *p = (Node *)malloc(sizeof(Node));
    p->ch = ch;
    p->freq = freq;
    p->lchild = lchild;
    p->rchild = rchild;
    return p;
}

void clear(Node *root) {
    if (root == NULL) return ;
    clear(root->lchild);
    clear(root->rchild);
    free(root);
    return ;
}

Heap *initHeap(int n) {
    Heap *h = (Heap *)malloc(sizeof(Heap));
    h->data = (Node **)malloc(sizeof(Node *) * (n + 5));
    h->n = n;
    h->cnt = 0;
    return h;
}

#define swap(a, b) { \
    __typeof(a) __c = a; \
    a = b, b = __c; \
}

int less(Node *n1, Node *n2) {
    return n1->freq < n2->freq;
}

void up_maintain(Heap *h, int ind) {
    if (ind == 1) return ;
    if (less(h->data[ind], h->data[ind >> 1])) {
        swap(h->data[ind], h->data[ind >> 1]);
        up_maintain(h, ind >> 1);
    }
    return ;
}

void down_maintain(Heap *h, int ind) {
    if ((ind << 1) > h->cnt) return ;
    int temp = ind;
    if (less(h->data[ind << 1], h->data[temp])) temp = ind << 1;
    if ((ind << 1 | 1) <= h->cnt &&
        less(h->data[ind << 1 | 1], h->data[temp])) {
        temp = ind << 1 | 1;
    }
    if (ind == temp) return ;
    swap(h->data[ind], h->data[temp]);
    down_maintain(h, temp);
    return ;
}

Node *top(Heap *h) { return h->data[1]; }
int empty(Heap *h) { return h->cnt == 0; }
int size(Heap *h) { return h->cnt; }

void push(Heap *h, Node *node) {
    h->data[++h->cnt] = node;
    up_maintain(h, h->cnt);
    return ;
}

void pop(Heap *h) {
    if (empty(h)) return;
    h->data[1] = h->data[h->cnt];
    h->cnt -= 1;
    down_maintain(h, 1);
    return ;
}

void clearHeap(Heap *h) {
    if (h == NULL) return ;
    free(h->data);
    free(h);
    return ;
}

Node *buildHalfman(Heap *h) {
    while (size(h) > 1) {
        Node *a = top(h); pop(h);
        Node *b = top(h); pop(h);
        push(h, getNewNode(0, a->freq + b->freq, a, b));
    }
    return top(h);
}

void getHalfmanCode(Node *root, int k, char *buff, char *code[]) {
    if (root == NULL) return ;
    buff[k] = 0;
    if (root->ch) {
        code[root->ch] = strdup(buff);
        return ;
    }
    buff[k] = '0';
    getHalfmanCode(root->lchild, k + 1, buff, code);
    buff[k] = '1';
    getHalfmanCode(root->rchild, k + 1, buff, code);
    return;
}

int main() {
    int n;
    scanf("%d", &n);
    Heap *h = initHeap(n);
    for (int i = 0; i < n; i++) {
        char ch[10];
        double freq;
        scanf("%s%lf", ch, &freq);
        push(h, getNewNode(ch[0], freq, NULL, NULL));
    }
    Node *root = buildHalfman(h);

    char *code[256] = {0}, buff[100];
    getHalfmanCode(root, 0, buff, code);
    for (int i = 1; i < 256; i++) {
        if (code[i] == NULL) continue;
        printf("%c %s\n", i, code[i]);
    }

    clearHeap(h);
    return 0;
}